package learnonline.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import learnonline.dao.CourseDao;
import learnonline.dao.RowMapper;
import learnonline.entity.Course;

public class CourseDaoImpl implements CourseDao{
	private JdbcImpl jdbc=new JdbcImpl();
	private static class CourseRowMapper implements RowMapper<Course>{

		@Override
		public Course mapRow(ResultSet rs) throws SQLException {
			Course c=new Course();
			c.setCid(rs.getInt("cid"));
			c.setCcontent(rs.getString("ccontent"));
			c.setCgrade(rs.getString("cgrade"));
			c.setChour(rs.getInt("chour"));
			c.setCname(rs.getString("cname"));
			c.setCprice(rs.getFloat("cprice"));
			c.setCtid(rs.getInt("tid"));
			c.setCimages(rs.getString("cimages"));
			c.setCteacher(rs.getString("tname"));
			return c;
		}
		
	}
	@Override
	public List<Course> getAllCourse() {
		String sql="select * from Course join teacher on course.ctid=teacher.tid";
		return jdbc.queryList(sql, new CourseRowMapper());
	}

	@Override
	public Course fetchById(int id) {
		String sql="select * from course where cid=?";
		List<Course> list=jdbc.queryList(sql, new CourseRowMapper(),id);
		return list.size()>0?list.get(0):null;
	}

}
